package xyz.tanxiao.links.common.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import xyz.tanxiao.links.common.entity.ResponseResult;

/**
 * 全局异常处理类
 *
 * @author 万成波
 * @email 2300064869@qq.com
 * @createTime 2023-06-08 22:04:09 星期四
 */
@ControllerAdvice
public class GlobalExceptionHandler {

	/**
	 * 处理转换短链异常
	 */
	@ResponseBody
	@ExceptionHandler(ToShortLinkException.class)
	public ResponseResult toShortLinkException(ToShortLinkException e) {
		return ResponseResult.error().message(e.getMessage());
	}


	/**
	 * 处理密码访问短链异常
	 */
	@ResponseBody
	@ExceptionHandler(PasswordAccessException.class)
	public ResponseResult passwordAccessException(PasswordAccessException e) {
		return ResponseResult.error().message(e.getMessage());
	}


	/**
	 * 处理普通访问短链异常
	 */
	@ExceptionHandler({GeneralAccessException.class, AccessLimitException.class})
	public ModelAndView accessShortLinkException(Exception e) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("fail");
		modelAndView.addObject("errorMessage", e.getMessage());
		return modelAndView;
	}


	/**
	 * 处理全局异常异常
	 */
	@ResponseBody
	@ExceptionHandler(Exception.class)
	public ResponseResult exception(Exception e) {
		return ResponseResult.error().message(e.getMessage());
	}

}
